//
//  JiFenController.m
//  ai2ban
//
//  Created by mac book pro on 2018/1/6.
//  Copyright © 2018年 mac book pro. All rights reserved.
//

#import "JiFenController.h"
#import "JFCollectionHeadReusableView.h"
#import "ComeStoreViewControl.h"
#import "JFenColltionCell.h"
#import "FictitiousViewController.h"
#import "CustomizationViewControl.h"
#import "FineMingControl.h"
#import "OffereController.h"
#import "JiFenGoodModel.h"
#import "JFCollectionHeadReusableView.h"
#import "GoodInfoController.h"


@interface JiFenController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (strong, nonatomic) UICollectionView *jfcollectionView;
@property (nonatomic, strong) JFCollectionHeadReusableView *headView;
//@property (nonatomic, strong) UICollectionReusableView *headView;
@property (strong, nonatomic) NSArray *jfFenGoods;
@end

@implementation JiFenController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"星星商城";
    self.view.frame = CGRectMake(0, 0, kScreen_Width, kScreen_Height - 49);
    self.view.backgroundColor = [UIColor whiteColor];
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(kScreen_Width / 2, kScreen_Width / 2 * 0.9);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    
    flowLayout.headerReferenceSize = CGSizeMake(0, 263);
    self.jfcollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    self.jfcollectionView.backgroundColor = [UIColor whiteColor];
    self.jfcollectionView.dataSource = self;
    self.jfcollectionView.delegate = self;
    [self.jfcollectionView registerNib:[UINib nibWithNibName:@"JFenColltionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"JFenCell"];
    [self.jfcollectionView registerNib:[UINib nibWithNibName:@"JFCollectionHeadReusableView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JFCollectionHeadReusableView"];
    
    [self.view addSubview:self.jfcollectionView];
    
    [self loadGood];
}

- (void) loadGood {
    [ActivityIndicatorView show];
    [[NetworkManager shareNetwork] requestWithURLString:@"app/system/getProductRecomelist" parameters:nil method:@"" callBack:^(id responseObject) {
        [ActivityIndicatorView dismiss];
        if ([responseObject[@"state"] intValue]) {
            self.jfFenGoods = [JiFenGoodModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
            [self.jfcollectionView reloadData];
            return;
        }
    }];
}

- (NSInteger)collectionView:(nonnull UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.jfFenGoods.count;
}

- (nonnull __kindof UICollectionViewCell *)collectionView:(nonnull UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    JFenColltionCell *cell =[collectionView dequeueReusableCellWithReuseIdentifier:@"JFenCell" forIndexPath:indexPath];
    cell.model = self.jfFenGoods[indexPath.item];
    return cell;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    if (kind == UICollectionElementKindSectionHeader) {
        self.headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"JFCollectionHeadReusableView" forIndexPath:indexPath];
        return self.headView;
    }
    return nil;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodInfoController *goodInfoCon = [[GoodInfoController alloc] init];
    goodInfoCon.goodModel = self.jfFenGoods[indexPath.row];
    [self.navigationController pushViewController:goodInfoCon animated:true];
}

- (IBAction)seeJiFenFineMingAction:(UIButton *)sender {
    FineMingControl *fmCon = [[FineMingControl alloc] init];
    [self.navigationController pushViewController:fmCon animated:true];
}

- (IBAction)fictitiousAction:(UIButton *)sender {
    FictitiousViewController *fictitiousControl = [[FictitiousViewController alloc] init];
    [self.navigationController pushViewController:fictitiousControl animated:true];
}

- (IBAction)customizationAction:(UIButton *)sender {
    CustomizationViewControl *customizationViewCon = [[CustomizationViewControl alloc] init];
    [self.navigationController pushViewController:customizationViewCon animated:true];
}

- (IBAction)comeStoreAction:(UIButton *)sender {
    ComeStoreViewControl *comeStore = [[ComeStoreViewControl alloc] init];
    [self.navigationController pushViewController:comeStore animated:true];
}

- (IBAction)offeredAction:(UIButton *)sender {
    OffereController *offedController = [[OffereController alloc] init];
    [self.navigationController pushViewController:offedController animated:true];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}


@end
